// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
/**
 * 大转盘
 */
let base = require("panelBase")
let viewConfig = {
    btn_back:{
        node:null,
        name:"btn_back",
        eventType:"click",
        cb:"onBtnBackClick"
    },
    remainCount:{
        node:null,
        name:"remain_count_bg/remainCount"
    },
    btn_start:{
        node:null,
        name:"tt/pointer",
        eventType:"click",
        cb:"onBtnStartClick"
    },
    pb:{
        node:null,
        name:"extra_reward/pb"
    },
    banner:{
        node:null,
        name:"banner",
        active:false,
    },
    banner_des:{
        node:null,
        name:"banner/des"
    },
    
    
};
cc.Class({
    extends: base,

    properties: {
        isSpinning:false,
    },

    init(){
        this._super(viewConfig);
        this.isInited = true;
        cc.tg.eventCenter.addEvent("onGetGGKCount",(data)=>{
            this.updateInfo(viewConfig["remainCount"].node,`剩余${data}次，次日更新`,cc.Label)
        })

        cc.tg.eventCenter.addEvent("onSpinResult",(data)=>{
          //  cc.log(data);
            cc.tg.cakeRoom.unuseCount--;
            this.updateInfo(viewConfig["remainCount"].node,`剩余${cc.tg.cakeRoom.unuseCount}次，次日更新`,cc.Label)
            // 礼盒没有中奖，金币，银币，红包
            // 金币 0,4 ，银币1,3,5, ，中奖7,没有中奖2,6
            let tPos = 0;
            let randomNum = Math.floor(Math.random()*2)
            if(data.winwinningState)
            {
                 tPos = 7 ;
            }
            else if(data.goldNumber > 0)
            {
                if(randomNum == 0)
                {
                    tPos = 0;
                }
                else
                {
                    tPos = 4;
                }
            }
            else if(data.sliverNumber > 0)
            {
                if(randomNum == 0)
                {
                    tPos = 1;
                }
                else if(randomNum == 1)
                {
                    tPos = 3
                }
                else
                {
                    tPos = 5;
                }
            }
            else {
                tPos = randomNum == 0 ? 2:6
            }
            //刷新用户信息
            cc.tg.gameLogic.toGetAccInfo();
            this.startSpin(tPos);
        })
        this.onHide();
    },
    onShow(){
        this._super();
        let btn = viewConfig["btn_start"].node;
        btn.angle = 0;
        cc.tg.adDataCenter.toShowAd("tt_5");
    },
    onBtnBackClick(){
        this.onHide();
    },

    onBtnStartClick(){
        cc.log("on btn start click");
        if(cc.tg.cakeRoom.unuseCount <= 0)
        {
            cc.log(" 剩余次数不足");
            return ;
        }
        cc.tg.gameLogic.toSpin();
      
    },

    startSpin(data){
        if(!data || this.isSpinning)
            return ;
        cc.log(data);
        let btn = viewConfig["btn_start"].node;
        btn.getComponent(cc.Button).interactable = false;
        this.updateInfo(btn.children[0],"",cc.Label)
        let endPos = data % 8 ;
        let round = 3 + Math.floor(Math.random()*5);
        let speed = 360 * 3;
        let totalAngle = -(round * 360 + endPos * 45 - 20);
        let dt = cc.delayTime(0.01);
        let cb = ()=>{
            totalAngle += speed * 0.01;
            btn.angle  -= speed*0.01;
            if(totalAngle >= 0)
            {
                this.onSpinEnd();
                this.toShowTTAd(data)
                //测试广告
                return ;
            }
            else if(totalAngle < 0 && totalAngle >= -360)
            {
                speed = 720;
            }
        }
        btn.runAction(cc.repeatForever(cc.sequence(dt,cc.callFunc(cb))));
        this.isSpinning = true;

    },

    onHide(){
        this._super();
        cc.tg.adDataCenter.toShowAd("tt_5",true);
    },
    onSpinEnd(){
        let btn = viewConfig["btn_start"].node;
        btn.stopAllActions();
        btn.angle = 0;
        btn.getComponent(cc.Button).interactable = true;
        this.updateInfo(btn.children[0],"立即抽奖",cc.Label)
        this.isSpinning = false;
    },
    //大转盘转完成之后显示广告
    toShowTTAd(pos){
        if(pos == null || pos < 0)
            return ;
        switch(pos)
        {
            case 0:
            case 4:
                cc.tg.eventCenter.dispatchEvent("toShowAd",{codeId:"tt_1",countDown:3});
                cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"banner",active:true})
               // this.onHide();
                break;
            case 1:
            case 3:
            case 5:
                cc.tg.eventCenter.dispatchEvent("toShowAd",{codeId:"tt_2_ex",countDown:3});
                cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"banner",active:true})
               // this.onHide();
                break;
            case 7:
                cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"ttHB",active:true})
                //this.onHide();
                break;
            case 2:
            case 6:
                cc.log("没有中奖");
            default:
                break;
        }

    },
    onDestroy(){
        for(let key in viewConfig)
        {
            viewConfig[key].node = null;
        }
    },
});
